package com.bai;


import com.sinoservices.hichain.scc.module.entity.Contract;
import com.sinoservices.hichain.scc.module.entity.OrderContractRelevantRecord;

import java.lang.reflect.Field;
import java.text.MessageFormat;

/**
 * @version Revision 1.0.0
 * @cLASSnAME CreateMybatilsXml
 * @版权：SINOSERVICES 版权所有 (c) 2013
 * @author:Felix
 * @email:Felix.Bai@sinoservices.com
 * @see:
 * @创建日期：2019/4/11 13:51
 * @功能说明：
 * @begin
 * @修改记录:
 * @end
 */
public class CreateMybatilsUpdateBatch {

    public static final String UPDATE_BATCH_BENGIN = "    <update id=\"updateBatch\" parameterType=\"java.util.List\">\n" +
            "       UPDATE {0}\n" +
            "        <trim prefix=\"set\" suffixOverrides=\",\">";
    public static final String UPDATE_BATCH_CENTER_CONDITION = "            <trim prefix=\"{0} =case\" suffix=\"end,\">\n" +
            "                <foreach collection=\"list\" item=\"item\" index=\"index\">\n" +
            "    when {1}=#'{'item.{2}'}' then #'{'item.{3}'}'\n" +
            "                </foreach>\n" +
            "            </trim>";
    public static final String UPDATE_BATCH_END ="        </trim>\n" +
            "        WHERE {0} in\n" +
            "        <foreach collection=\"list\" index=\"index\" item=\"item\" separator=\",\" open=\"(\" close=\")\">\n" +
            "            #'{'item.{1}'}'\n" +
            "        </foreach>\n" +
            "    </update>";

    public static void main(String[] args) {

        createUpdateBatch(Contract.class);
        createUpdateBatch(OrderContractRelevantRecord.class);
    }

    public static void createUpdateBatch(Class clazz){
        String className = clazz.getSimpleName();
        String tableName = formatToSqlName(className).substring(1);
        String mainKey_FILED = lowerFirst(className)+"Id";
        String mainKey_SQL = formatToSqlName(mainKey_FILED);

        Field[] fields = clazz.getDeclaredFields();
        System.out.println(MessageFormat.format(UPDATE_BATCH_BENGIN,tableName));
        for(int i = 0 ; i < fields.length ; i++) {
            if(mainKey_FILED.equals(fields[i].getName())){
                continue;
            }
            System.out.println(MessageFormat.format(UPDATE_BATCH_CENTER_CONDITION,formatToSqlName(fields[i].getName()),mainKey_SQL,mainKey_FILED,fields[i].getName()));
        }
        System.out.println(MessageFormat.format(UPDATE_BATCH_END,mainKey_SQL,mainKey_FILED));
    }


    public static String formatToSqlName(String fieldName){
        StringBuffer sb = new StringBuffer();
        char[] ch = fieldName.toCharArray();
        for (char c:ch){
            sb.append(upChar(c));
        }
        return sb.toString();
    }

    public static String upChar(char c){
        if(c>='a' && c<='z') {
            return String.valueOf(c).toUpperCase();
        }else if(c>='A' && c<='Z') {
            return "_"+String.valueOf(c).toUpperCase();
        }else if(c>='0' && c<='9'){
            return String.valueOf(c).toUpperCase();
        }else{
            return "";
        }
    }

    public static String lowerFirst(String name){
        char[] chars = name.toCharArray();
        chars[0] += 32;
        return String.valueOf(chars);
    }

}
